<?php

namespace app\service\home;


use app\model\Common;
use GatewayWorker\Lib\Gateway;
use think\facade\Cache;
use think\facade\Session;

class ChatService
{
    protected $roomId;
    public $name;
    public $listName;
    public $clientList = 'client_list';
    public $expire = 86400;

    public function __construct($roomId, $name)
    {
        $this->roomId = $roomId;
        $this->name = $name;
        $this->listName = 'room_' . $roomId;
        Gateway::$registerAddress = '127.0.0.1:1236';
    }

    /**
     * 绑定用户 sessionid绑定client_id
     * @param string $clientId
     * @return bool
     * @author Du
     * @date 2021/3/8 14:28
     */
    public function bingUser(string $clientId): bool
    {
        $sessionId = Session::getId();
        Gateway::bindUid($clientId, $sessionId);
        Gateway::joinGroup($clientId, $this->roomId);
        // 转播给当前房间的所有客户端，xx进入聊天室 message {type:login, client_id:xx, name:xx}
        $this->sendData('addNotice', $sessionId);
        $redis = Cache::store('redis')->handler(); // 这条代码等于  $redis = new \Redis();
        $data = [
            'name' => $this->name,
            'session_id' => $sessionId,
            'room_id' => $this->roomId,
            'client_id' => $clientId,
        ];
        $redis->setex($clientId, $this->expire, arrayToJson($data));
        $redis->hset($this->listName, $sessionId, arrayToJson($data));
        return true;
    }

    /**
     * 记录聊天记录
     * @param string $content
     * @return bool
     * @author Du
     * @date 2021/3/8 10:15
     */
    public function addSpeak(string $content): bool
    {
        $data = [
            'room_id' => $this->roomId,
            'user_name' => $this->name,
            'content' => $content,
            'create_time' => time(),
        ];
        return $this->sendData('speak', Session::getId(), $content);
//        return Common::addData(CHATSPEAKLOGTABLE, $data);
    }

    /**
     * 返回房间用户列表
     * @return mixed
     * @author Du
     * @date 2021/3/10 17:00
     */
    public function getAllGroupList()
    {
        $redis = Cache::store('redis')->handler(); // 这条代码等于  $redis = new \Redis();
        $list = $redis->hvals($this->listName);
        if ($list) {
            foreach ($list as &$v) {
                $v = json_decode($v, true);
            }
        }
        return $list;
    }

    /**
     * 用户断开链接
     * @param array $data
     * @param string $clientId
     * @return bool
     * @author Du
     * @date 2021/3/16 13:53
     */
    public function disconnect(array $data, string $clientId): bool
    {
        Gateway::leaveGroup($clientId, $this->roomId);
        $this->sendData('leaveNotice', $data['client_id']);
        $redis = Cache::store('redis')->handler();
        $redis->hdel($this->listName, $data['session_id']);
        return true;
    }

    /**
     * 发送通知
     * @param string $type
     * @param string $clientId
     * @param string $content
     * @return bool
     * @author Du
     * @date 2021/3/16 14:24
     */
    public function sendData(string $type, string $clientId, string $content = ''): bool
    {
        // 转播给当前房间的所有客户端，xx进入聊天室 message {type:login, client_id:xx, name:xx}
        $newMessage = [
            'type' => $type,
            'client_id' => $clientId,
            'client_name' => $this->name,
            'time' => date('Y-m-d H:i:s')
        ];
        if ($content) {
            $newMessage['content'] = $content;
        }
        Gateway::sendToGroup($this->roomId, arrayToJson($newMessage));
        return true;
    }
}
